import request from "../utils/request";
export default {

  methods: {
    absUrl(url) {
      return this.$absUrl(url);
    },
    goodsStatus(status) {
      if (status == -1) {
        return '已取消'
      }
      let map = {
        '1': '待发货',
        '2': '待收货',
        '4': '待评论',
        '8': '已评论',
        '16': '已退款',
        '32': '退款中',
        '64': '退款失败',
        "-1": "已取消",
        '128': '申请退款中',
        '256': '退款拒绝',
      };
      return map[status+ '']
    },
    imgUrl(url) {
      if (!url) {
        return url;
      }
      if (url.indexOf('://') != -1 || url.indexOf('//') === 0) {
        return url;
      }
      if (url[0] != '/') {
        url = '/' + url;
      }
      if (url.indexOf('/images/') != -1) {
        return url;
      }
      if(url.indexOf('/oss-image/')!=-1){
        return 'http://weixin-ostorage01.oss-cn-shenzhen.aliyuncs.com'+url;
     }
      return 'https://weixina01.bppc.com.cn/zy-admin/UploadFiles/zy/product' + url;
    },
    trim(str, char) {
      return str.trim().replace(new RegExp("^" + char + "*|" + char + "*$"), '');
    },
    parse2date(time, cFormat) {
      if (arguments.length === 0) {
        return null
      }

      if ((time + '').length === 10) {
        time = +time * 1000
      }

      const format = cFormat || '{y}-{m}-{d}'
      let date
      if (typeof time === 'object') {
        date = time
      } else {
        date = new Date(parseInt(time))
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
      }
      const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
        if (result.length > 0 && value < 10) {
          value = '0' + value
        }
        return value || 0
      })
      return time_str
    },
    $success(msg){
      this.$toast({
        message: msg || '操作成功',
        iconClass: 'icon icon-success'
      });
    },
    $assert(method, value, msg) {
      if (method == 'isEmpty') {
        let rt = !value || value.trim().length == 0;
        if (rt && msg) {
          this.$toast(msg);
        }
        return rt;
      }
      return false;
    },
    isEmpty(str) {
      if (!str) {
        return true;
      }
      if (typeof str === 'string') {
        return str.trim().length == 0;
      }
      if (Array.isArray(str)) {
        return str.length == 0;
      }
      for (const key in str) {
        if (str.hasOwnProperty(key)) {
          return false;
        }
      }
      return false;
    },
    isweixin() {
      const ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
      } else {
        return false;
      }
    },
    noop() {

    },
    $confirm(message, title) {
      return this.$messagebox.confirm(message, title);
    },
    $absUrl(url) {
      return url;
      //   if (url.indexOf("://") != -1) {
      //       return url;
      //   }
      //   if(url[0] != '/'){
      //     url=  '/' + url;
      //   }
      //   return 'http://'
    },
    showLoading() {
      this.$indicator.open();
    },
    hideLoading() {
      this.$indicator.close();
    },
    $get(url, params) {
      return request({
        url: this.$absUrl(url),
        method: 'get',
        params
      })
    },
    $post(url, data) {
      return request({
        url: this.$absUrl(url),
        method: 'post',
        data
      })
    }
  }
};
